package com.github.jspxnet.worktask.util;

import com.github.jspxnet.boot.environment.Environment;
import com.github.jspxnet.enums.AuditEnumType;
import com.github.jspxnet.scriptmark.ScriptmarkEnv;
import com.github.jspxnet.scriptmark.core.ScriptMarkEngine;
import com.github.jspxnet.scriptmark.load.Source;
import com.github.jspxnet.scriptmark.load.StringSource;
import com.github.jspxnet.security.utils.EncryptUtil;
import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.utils.DateUtil;
import com.github.jspxnet.utils.ObjectUtil;
import com.github.jspxnet.utils.StringUtil;
import com.github.jspxnet.worktask.model.dto.ConfFlowJobDto;
import com.github.jspxnet.worktask.model.dto.ConfFlowNodeDto;
import com.github.jspxnet.worktask.model.dto.ConfWorkFlowDto;
import com.github.jspxnet.worktask.model.enums.FlowNodeEnumType;
import com.github.jspxnet.worktask.model.param.WorkTaskParam;
import com.github.jspxnet.worktask.table.ConfFlowNode;
import com.github.jspxnet.worktask.table.ConfWorkFlow;
import com.github.jspxnet.worktask.table.UserFlowLog;
import com.github.jspxnet.worktask.table.WorkTask;

import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public final class WorkFlowUtil {
    private WorkFlowUtil()
    {

    }

    /**
     *
     * @param userFlowLogList 用户留言列表
     * @return 得到审核通过数量
     */
    public static int getAuditCount(List<UserFlowLog> userFlowLogList)
    {
        if (ObjectUtil.isEmpty(userFlowLogList))
        {
            return 0;
        }
        int i = 0;
        List<String> list = new ArrayList<>();
        for (UserFlowLog userFlowLog:userFlowLogList)
        {
            String key  = userFlowLog.getPutUid()<=0?userFlowLog.getPutName(): ObjectUtil.toString(userFlowLog.getPutUid());
            if (AuditEnumType.OK.getValue()==userFlowLog.getAuditType()&&!list.contains(key))
            {
                list.add(key);
            }
        }
        return list.size();
    }

    public static ConfFlowNode getStartConfWorkFlow(ConfWorkFlow confWorkFlow)
    {
        if (confWorkFlow==null)
        {
            return null;
        }
        List<ConfFlowNode> list = confWorkFlow.getNodeList();
        if (ObjectUtil.isEmpty(list))
        {
            return null;
        }
        for (ConfFlowNode node:list)
        {
            if (FlowNodeEnumType.START.getValue()==node.getFlowNodeType())
            {
                return node;
            }
        }
        return null;
    }

    /**
     * 调用模板引擎
     *
     * @param x       模板字符串
     * @param map     参数
     * @return 返回执行结果
     * @throws Exception 异常
     */
    public static String runScript(String x, Map<String,Object> map) throws Exception {

        //这里演示了如何调用模板引擎
        Source reader = new StringSource(x); //这里可以是文件 FileSource  JarSource
        ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine(ScriptmarkEnv.noCache, reader, null);
        Writer writer = new StringWriter();
        scriptMarkEngine.process(writer, map);
        writer.close();
        return writer.toString();
    }


    /**
     *
     * @param currentWorkTask 参数
     * @return 生成变量ID
     */
    public static String getVariableNo(WorkTask currentWorkTask)
    {
        String documentNo = currentWorkTask.getDocumentNo();
        String lastNo = StringUtil.empty;
        if (documentNo!=null&&documentNo.length()>3)
        {
            lastNo = documentNo.substring(documentNo.length()-3);
        }
        String sb = currentWorkTask.getNamespace() +
                currentWorkTask.getFlowNo() +
                currentWorkTask.getDocumentNo();
        String str = EncryptUtil.getMd5(sb);
        return str.substring(0,str.length()-2) + lastNo;
    }


    /**
     *
     * @param joinNodeList 合并的节点列表
     * @param taskNodeList  已经执行的任务
     * @return 返回没有执行的节点
     */
    public static List<String> getNotExeNodeList(List<String> joinNodeList,List<String> taskNodeList)
    {
        //取差集
        return joinNodeList.stream().filter(t-> !taskNodeList.contains(t)).collect(Collectors.toList());
    }

    public static ConfWorkFlowDto convertDto(ConfWorkFlow confWorkFlow)
    {
        ConfWorkFlowDto confWorkFlowDto = BeanUtil.copy(confWorkFlow, ConfWorkFlowDto.class);
        List<ConfFlowNodeDto> nodeList = new ArrayList<>();
        for (ConfFlowNode confFlowNode:confWorkFlow.getNodeList())
        {
            ConfFlowNodeDto node = BeanUtil.copy(confFlowNode, ConfFlowNodeDto.class);
            ConfFlowJobDto confFlowJob = BeanUtil.copy(confFlowNode.getConfFlowJob(), ConfFlowJobDto.class);
            if (!StringUtil.isNull(confFlowJob.getNodeId()) && !StringUtil.isNull(confFlowJob.getFlowNo()))
            {
                node.setConfFlowJob(confFlowJob);
            } else
            {
                node.setConfFlowJob(null);
            }
            nodeList.add(node);
        }
        confWorkFlowDto.setNodeList(nodeList);
        return confWorkFlowDto;
    }


}
